package luzhin;

import static luzhin.Board.*;

import java.io.*;

/**
 * Static class for operations on positions on the chessboard. Positions are numbered from 0 to 63.
 * @author lacungus
 *
 */
public final class Position implements Serializable {
	private static final long serialVersionUID = 23908123908312L;

    public static int INVALID = -1;
	/**
	 * 
	 * @param s A string representation of a position.
	 * @return Number representation of the position.
	 */
	public static int getPosition(String s) {
		int i = s.charAt(1) - '1';
		int j = s.charAt(0) - 'a';
		return i * LEN + j;
	}
	
	/**
	 * 
	 * @param pos
	 * @return Color of the position (0 for white, 1 for black).
	 */
	public static int getColor(int pos) {
		int i = pos / Board.LEN;
		int j = pos % Board.LEN;
		return ((i % 2) + (j % 2)) % 2;
	}

    public static boolean isInvalidXY(int xy) {
        return xy < 0 || xy >= LEN;
    }
	
	/**
	 * 
	 * @param pos
	 * @return
	 */
	public static String positionToString(int pos) {
		int i = pos / LEN;
		int j = pos % LEN;
		String s = "";
		s += (char) ('a' + j);
		s += (char) ('1' + i);
		return s;
	}
	
	/**
	 * 
	 * @return
	 */
	public static String positionToString(int i, int j) {
		String s = "";
		s += (char) ('a' + j);
		s += (char) ('1' + i);
		return s;
	}
	
	/**
	 * 
	 * @param i Row.
	 * @param j Column.
	 * @return
	 */
	public static int getPosition(int i, int j) {
		if (i < 0 || j < 0 || i >= Board.LEN || j >= Board.LEN)
			return INVALID;
		return i * Board.LEN + j;
	}
}
